package com.bbz.core.lambda.java8.Chapter_12_Date;


import java.time.*;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;
import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.nextOrSame;

/**
 * Java8 Date Demo
 */
public class DateDemo {
    public static void main(String args[]){
        LocalDate date=LocalDate.of(2018,03,25);
        int year = date.getYear();
        System.out.println("year："+year);
        Month month = date.getMonth();
        System.out.println("Month："+month.getValue());
        int day = date.getDayOfMonth();
        System.out.println("day："+day);
        DayOfWeek dow = date.getDayOfWeek();
        System.out.println("休息日："+dow.getValue());
        int len = date.lengthOfMonth();
        System.out.println("这个月有多少天："+len);
        boolean leap = date.isLeapYear();
        System.out.println("是不是闰年："+leap);
        LocalDate today = LocalDate.now();
        System.out.println("当前时间："+today);

        //使用TemporalField读取LocalDate的值 等效 date.getYear(),date.getMonth
        year = date.get(ChronoField.YEAR);
        System.out.println("year："+year);
        int month1 = date.get(ChronoField.MONTH_OF_YEAR);
        System.out.println("Month："+month1);
        day = date.get(ChronoField.DAY_OF_MONTH);
        System.out.println("day："+day);

        //创建LocalTime并读取其值
        LocalTime time=LocalTime.of(10,30,59);
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
        //格式化
        LocalDate date1 = LocalDate.parse("2014-03-18");
        System.out.println(date1);
        LocalTime time1 = LocalTime.parse("13:45:20");
        System.out.println(time1);
        ZoneId z = ZoneId.of("UTC");
        System.out.println("时区:"+LocalDateTime.now(z));

        //直接创建LocalDateTime对象，或者通过合并日期和时间的方式创建
        LocalDateTime dt1=LocalDateTime.of(2018,9,7,9,20,10);
        System.out.println(dt1);
        LocalDateTime dt2 = LocalDateTime.of(date, time);
        System.out.println(dt2);
        //LocalData 追加 LocalTime
        LocalDateTime dt3 = date.atTime(13, 45, 20);
        LocalDateTime dt4 = date.atTime(time);
        LocalDateTime dt5 = time.atDate(date);

        //从LocalDateTime中获取LocalDate或LocalTime
        LocalDate date2 = dt1.toLocalDate();
        LocalTime time2 = dt1.toLocalTime();

        //通过Duration 比对时间,Duration类主要用于以秒和纳秒衡量时间的长短，你不能仅向between方法传递一个LocalDate对象做参数。
        Duration d1 = Duration.between(time1, time2);
        System.out.println(d1.getSeconds());
        //通过Period 比对时间
        Period tenDays = Period.between(LocalDate.of(2014, 3, 8),
                LocalDate.of(2014, 3, 18));
        System.out.println(tenDays.getDays());
        //操作LocalDate 修改时间 利用withYear,withDayOfMonth等等方法不会直接对date1改变，而是产生副本。
        LocalDate date5 = LocalDate.of(2014, 3, 18);
        LocalDate date6 = date5.withYear(2011);
        LocalDate date7 = date6.withDayOfMonth(25);
        LocalDate date8 = date7.with(ChronoField.MONTH_OF_YEAR, 9);
        //操作LocalDate 增加时间
        LocalDate date9 = LocalDate.of(2014, 3, 18);
        System.out.println("增加两天****"+date9.plusDays(2));
        LocalDate date10 = date9.plusWeeks(1);
        LocalDate date11 = date10.minusYears(3);
        LocalDate date12 = date11.plus(6, ChronoUnit.MONTHS);
        //通过TemporalAdjuster 实现复杂时间操作.TemporalAdjuster 里有很多静态方法。
        LocalDate date13 = LocalDate.of(2014, 3, 18);
        LocalDate date14 = date13.with(nextOrSame(DayOfWeek.SUNDAY));
        System.out.println(date14);
        LocalDate date15 = date14.with(lastDayOfMonth());
        System.out.println(date15);
        //自定义实现 TemporalAdjuster 接口
        System.out.println(date.with(c->new NextWorkingDay().adjustInto(c)));
        //利用DateTimeFormatter 格式化时间
        LocalDate date16=LocalDate.of(1994,03,25);
        String str1=date16.format(DateTimeFormatter.BASIC_ISO_DATE); //19940325
        System.out.println(str1);
        String str2=date16.format(DateTimeFormatter.ISO_LOCAL_DATE); //1994-03-25
        System.out.println(str2);
        //利用DateLocal.parse 格式化时间
        LocalDate date17 = LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate date18 = LocalDate.parse("2014-03-18",DateTimeFormatter.ISO_LOCAL_DATE);
        //利用DateTimeFormatter创建模板
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date19=LocalDate.of(2019,03,25);
        String str3=date19.format(dateTimeFormatter);
        System.out.println(str3);
        //构建DateTimeFormatter模板
        DateTimeFormatter dateTimeFormatter1=new DateTimeFormatterBuilder()
                .appendText(ChronoField.DAY_OF_MONTH)
                .appendLiteral(". ")
                .appendText(ChronoField.MONTH_OF_YEAR)
                .appendLiteral(" ")
                .appendText(ChronoField.YEAR)
                .parseCaseInsensitive()
                .toFormatter(Locale.ITALIAN);
        LocalDate date20=LocalDate.of(2019,03,25);
        System.out.println(date20.format(dateTimeFormatter1));

        LocalDate localDateTim=LocalDate.parse("2018-11-02T10:20:00".split("T")[0]);
        DateTimeFormatter dateTimeFormatter2 = new DateTimeFormatterBuilder()
                .appendValue(YEAR)
                .appendLiteral('-')
                .appendValue(MONTH_OF_YEAR, 2)
                .appendLiteral('-')
                .appendValue(DAY_OF_MONTH, 2)
                .appendLiteral("T00:00:00")
                .toFormatter(Locale.ITALIAN);
        localDateTim.format(dateTimeFormatter2);
        System.out.println(localDateTim);

        LocalDate localDateTim1=LocalDate.parse("2018-11-02T10:20:00".split("T")[0].concat("T00:00:00"));
        System.out.println(localDateTim1);
    }
}
